<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>[[${sjcd}]]-[[${ejcd}]] - [[${T(com.b2c.erp.DataConfigObject).getInstance().getSysName()}]]</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">

<div class="layui-body" style="padding:15px;top: 0px;">

    <!-- start sp-wrapper -->
    <link rel="stylesheet" href="/sb/stock.css"/>
    <div class="sp-wrapper">
        <h2 class="sp-title"><span th:text="${sjcd}"></span></h2>
        <div class="sp-tool">
            <span class="fl">
                <form action="/purchase/list" method="get">
                <b><input type="text" name="billNo" th:value="${billNo}" placeholder="输入单据编号查询" style="width:200px;"></b>
                <b>
                    <select name="transType">
                        <option value="">选择采购类型</option>
                        <option value="Purchase">入库采购</option>
                        <option value="OrderDaiFa">订单代发采购</option>
                    </select>
                </b>
                <b>
                    <select name="contactId">
                        <option value="">供应商</option>
                        <option th:each="item:${contacts}" th:value="${item.id}" th:text="${item.name}">入库采购</option>
                  
                    </select>
                </b>
                <b>单据日期：
                    <input type="text" name="startDate" autocomplete="off" style="width: 120px" th:value="${startDate}" class="startDate"> -
                    <input type="text" th:value="${endDate}" name="endDate" class="endDate" style="width: 120px" autocomplete="off">
                    <input type="submit" class="button" value="查询"></b>
                    <b>总共：&nbsp;[[${totalSize}]]&nbsp;条记录</b>
                </form>

            </span>

            <span class="fr">
                <a href="/purchase/add" class="add">新增采购单</a>
                <!--                <i>导出</i>-->
            </span>
        </div>
        <div class="sp-table">

            <table>
                <thead>
                <tr>
                    <th>采购单ID</th>
                    <th>单据日期</th>
                    <th>单据编号</th>
                    <th>合同号</th>
                    <th>类型</th>
                    <th>供应商</th>
                    <th>采购总数</th>
                    <th>已入库数量</th>
                    <th>购货总金额</th>
<!--                    <th>购货金额</th>-->
<!--                    <th>优惠后金额</th>-->
                    <th>已付款</th>
<!--                    <th>付款状态</th>-->
                    <!--<th>数量</th>-->
                    <th>制单人</th>
<!--                    <th>审核人</th>-->
                    <th>状态</th>
                    <th>核销状态</th>
                    <th>备注</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="sp-data">
                <tr th:each="item:${list}">
                    <td th:text="${item.id}">2019-03-15</td>
                    <td th:text="${item.billDate}">2019-03-15</td>
                    <td th:text="${item.billNo}">CG201903121700193</td>
                    <td th:text="${item.contractNo}">CG201903121700193</td>
                    <td><b th:text="${T(com.b2c.entity.erp.enums.InvoiceTransTypeEnum).getName(item.transType)}"></b></td>
                    <td th:text="${item.contactName}">华衣</td>
                    <td th:text="${item.totalQuantity}">0</td>
                    <td th:text="${item.inQuantity}">0</td>
                    <td th:text="${#numbers.formatDecimal(item.totalAmount, 1, 2)}">0</td>
                    <!--<td th:text="${#numbers.formatDecimal(item.totalAmount, 1, 2)}">1000.00</td>-->
                    <!--<td th:text="${#numbers.formatDecimal(item.amount, 1, 2)}">900.00</td>-->
                    <td th:text="${#numbers.formatDecimal(item.rpAmount, 1, 2)}">500.00</td>
<!--                    <td th:switch="${item.rpAmount}">-->
<!--                        <b th:case="0">未付款</b>-->
<!--                        <b th:case="item.amount">已付款</b>-->
<!--                        <b th:case="*">部分付款</b>-->
<!--                    </td>-->
                    <!--<td th:text="${item.totalQuantity}">500</td>-->
                    <td th:text="${item.userName}">华衣</td>
<!--                    <td th:text="${item.checkName}">华衣</td>-->
                    <td th:text="${T(com.b2c.entity.erp.enums.InvoiceBillStatusEnum).getName(item.billStatus)}">华衣</td>
                    <td >
                        <b th:text="${T(com.b2c.entity.erp.enums.InvoiceHxStateCodeEnum).getName(item.hxStateCode)}"></b>
                        <a th:if="${T(com.b2c.entity.erp.enums.InvoiceHxStateCodeEnum).Payed.getIndex() != item.hxStateCode}" th:attr="data-id=${item.getId()},rpAmount=${item.rpAmount},totalAmount=${item.totalAmount}" class="layui-btn layui-btn-warm layui-btn-sm pay_btn">付款</a>
                    </td>
                    <td th:text="${item.description}">收到货请检查</td>
                    <td>
                        <a th:if="${item.getBillStatus()==0}" th:attr="erpInvoiceId=${item.getId()}"  href="javascript:;" class="purCheck">审核</a>
<!--                        <a  href="javascript:;" th:attr="erpInvoiceId=${item.getId()}" class="createHt">生成合同</a>-->
                        <i class="print" th:attr="data-id=${item.id}">打印采购详情</i>
<!--                      <a th:href="${'/purchase/detail?id='+item.id}" target="_blank">详情</a>-->
<!--                        <i class="delete" th:attr="data-id=${item.id}">删除</i>-->
                        <!--<a th:href="@{/purchase/checkout_form(id=${item.id})}">验货</a>-->
                       <a th:href="@{/purchase/stock_in(id=${item.id})}" th:if="${item.checked == 1 && item.billStatus == 1 && T(com.b2c.entity.erp.enums.InvoiceTransTypeEnum).Purchase.getIndex() == item.transType }" >入库</a>
<!--                        <a th:href="@{/stock_in/purchase_in_list(billNo=${item.billNo})}" >入库记录1</a>-->
                        <a th:href="@{/stock_in/stock_in_form_list(type=1,iid=${item.id})}" th:if="${item.billStatus == 3 && T(com.b2c.entity.erp.enums.InvoiceTransTypeEnum).Purchase.getIndex() == item.transType }">入库记录</a>
                        <!--<a target="_blank" th:href="@{/purchase/purchase_cancel(id=${item.id})}">采购退货</a>-->
                    </td>
                </tr>

                </tbody>
            </table>

        </div>
        <div class="pages">
            <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
        </div>
    </div>

    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
  <!--  <script type="text/javascript">
        $(function () {
            // 删除
            $('.delete').click(function () {
                var obj = $(this).parent().parent();
                var id = $(this).attr('data-id');
                Tips.confirm({
                    title: '删除记录',
                    content: '您确定要删除该购货记录吗？',
                    define: function () {

                        // ajax处理invoice_delete
                        $.ajax({
                            url: "/ajax_purchase/invoice_delete",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify({id: id}),
                            success: function (res) {
                                // alert(res.data.length);
                                if (res.code == 0) {
                                    Tips.alert('删除成功', function () {
                                        // location.reload(true);
                                        $(obj).remove();
                                    });

                                }
                                // alert(JSON.stringify(res));
                            }
                        });

                        // .. id
                        // Tips.alert('删除成功', function () {
                        //     location.reload(true);
                        // });
                    }
                });
            });


        });
    </script>-->

    <script type="text/javascript">
        $(function () {
            $('.purCheck').click(function () {
                var id = $(this).attr('erpInvoiceId');
                Tips.confirm({
                    title: '采购审核',
                    content: '您确定要审核通过吗？',
                    define: function () {
                        $.ajax({
                            url: "/ajax_purchase/purchase_check",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify({erpInvoiceId: id,status:1}),
                            success: function (res) {
                                if (res.code == 0) {
                                    location.reload();
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                    }
                });
            });
            $('.createHt').click(function () {
                var id = $(this).attr('erpInvoiceId');
                window.location.href = "/stock_in/purchase_in_ht_export?id="+id;
            });
        });
    </script>

    <!-- end sp-wrapper -->
    <script>
        layui.use(['laydate', 'layer'], function () {
            var laydate = layui.laydate;
            var layer = layui.layer;
            //执行一个laydate实例
            laydate.render({
                elem: '.startDate' //指定元素
            });
            laydate.render({
                elem: '.endDate' //指定元素
            });

            $(".pay_btn").click(function(){
                var id = $(this).attr("data-id");
                var rpAmount = $(this).attr("rpAmount");
                var totalAmount = $(this).attr("totalAmount");

                console.log(id);
                layer.prompt({
                    formType: 3,
                    value: rpAmount,
                    title: '请输入付款金额'
                }, function(value, index, elem){
                    var hasAm = totalAmount - rpAmount;
                   
                    // if(value > hasAm ){
                    //     layer.alert("支付金额不对");
                    // }else{
                        var data = {id:id, payAmount:value};
                        $.ajax({
                            url: "/purchase_ajax/payAmount",
                            type: "POST",
                            dataType: "JSON",
                            contentType: 'application/json',
                            data:JSON.stringify(data),
                            success: function (res) {

                                if(res.code == 0){
                                    layer.alert(res.msg);
                                    location=location;
                                }else
                                    layer.alert(res.msg);
                            }
                        });
                    // }


                    //layer.close(index);
                    //layer.alert(value);
                    // $.ajax({
                    //     type: "post",
                    //     url: "/visit/updateVisitApply",
                    //     data: {
                    //         "id": id,
                    //         "status": "3",
                    //         "reason":value
                    //
                    //     },
                    //     async: true,
                    //
                    // });
                    // layer.alert('驳回成功');
                    // location=location;
                });
            });


            $(".print").click(function () {
                var id = $(this).attr("data-id");
                console.log(id);
                //iframe层

                layer.open({
                    type: 2,
                    title: '打印采购单',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['940px', '90%'],
                    content: '/purchase/detail?id=' + id //iframe的url
                });
            })

        });
    </script>
</div>
</body>
<html>
